//
//  ViewController.m
//  10-Homework
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CarNewsModel.h"
#import "ImagesTableViewCell.h"
#import "NewsTableViewCell.h"
#import "UIView+ChangeFrame.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define kTableViewReuse @"reuseIndentifier"
#define kImagesTableViewReuse @"imagesReuse"
#define kNewsTableViewReuse @"newsReuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *carNewsModelArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.hidden = YES;
    self.carNewsModelArr = [NSMutableArray array];
    [self getData];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    [_tableView registerClass:[ImagesTableViewCell class] forCellReuseIdentifier:kImagesTableViewReuse];
    [_tableView registerClass:[NewsTableViewCell class] forCellReuseIdentifier:kNewsTableViewReuse];
    [self.view addSubview:_tableView];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [self.view addSubview:topView];
    UIScrollView *scollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    scollView.backgroundColor = [UIColor cyanColor];
    scollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    [topView addSubview:scollView];
    NSArray *titleArr = @[@"贷款买车", @"直销车型", @"底价买车", @"二手车"];
    for (int i = 1; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + WIDTH * 0.18 * (i - 1) + 25 * (i - 1) , scollView.z_height, WIDTH * 0.18, 50);
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]] forState:UIControlStateNormal];
        [topView addSubview:button];
    }
    [_tableView beginUpdates];
    _tableView.tableHeaderView = topView;
    [_tableView endUpdates];
}

- (void)getData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSDictionary *dataDic = [dic objectForKey:@"data"];
    NSArray *listArr = [dataDic objectForKey:@"list"];
    for (NSDictionary *dic in listArr) {
        CarNewsModel *carModel = [CarNewsModel carNewsModelWithDictionary:dic];
        [_carNewsModelArr addObject:carModel];
    }
}

#pragma mark - Tableview Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _carNewsModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarNewsModel *model = _carNewsModelArr[indexPath.row];
    NSString *type = [NSString stringWithFormat:@"%@", model.type];
    if ([type isEqualToString:@"3"]) {
        ImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kImagesTableViewReuse];
        return cell;
    } else {
        NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsTableViewReuse];
        cell.carNewsModel = model;
        return cell;
    }
}
#pragma mark - TableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
